package com.example.composableexample.custom.animation

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 * author: LiLiang
 * createTime: 2022/10/8-11:50
 * remark:
 **/

@Preview
@Composable
fun PacmanAnimation() {
    val infiniteTransition = rememberInfiniteTransition()
    val mouthAnimation by infiniteTransition.animateFloat(
        initialValue = 360f,
        targetValue = 280f,
        animationSpec = infiniteRepeatable(
            animation = tween(
                durationMillis = 800,
                easing = FastOutSlowInEasing
            ), repeatMode = RepeatMode.Restart
        )
    )
    val antiMouthAnimation by infiniteTransition.animateFloat(
        initialValue = 0f,
        targetValue = 40f,
        animationSpec = infiniteRepeatable(
            animation = tween(
                durationMillis = 800,
                easing = FastOutSlowInEasing
            ), repeatMode = RepeatMode.Restart
        )
    )
    Canvas(
        modifier = Modifier
            .padding(top = 16.dp)
            .size(100.dp)
    ) {
        drawArc(
            color = Color(0xffffd301),
            startAngle = antiMouthAnimation,
            sweepAngle = mouthAnimation,
            useCenter = true
        )
        drawCircle(
            color = Color.Black,
            radius = 15f,
            center = Offset(x = this.center.x + 15f, y = this.center.y - 85f)
        )
    }
}